<template>
  <div class="sys-api-page">
    <el-card
      class="aui-card--fill"
      shadow="never">
      <div class="mod-sys__user">
        <el-form
          :inline="true"
          :model="dataForm"
          @keyup.enter.native="getDataList()"
        >
          <el-form-item>
            <el-select
              @change="getDataList"
              clearable
              filterable
              :placeholder="$t('monitor.ip')"
              v-model="dataForm.ip"
            >
              <el-option
                :key="item.id"
                :label="item.label"
                :value="item.value"
                v-for="item in ipList"
              />
            </el-select>

            <!--                        <el-input :placeholder="$t('monitor.ip')" clearable v-model="dataForm.ip"></el-input>-->
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()">{{ $t("query") }}</el-button>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="addOrUpdateHandle()"
              type="primary"
            >{{ $t("add") }}
            </el-button>
          </el-form-item>
          <!--                <el-form-item>-->
          <!--                    <el-button v-if="$hasPermission('sys:user:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>-->
          <!--                </el-form-item>-->
          <!--                    <el-form-item>-->
          <!--                        <el-button v-if="$hasPermission('sys:user:export')" type="info" @click="exportHandle()">{{ $t('export') }}</el-button>-->
          <!--                    </el-form-item>-->
        </el-form>
        <el-table
          :data="dataList"
          @selection-change="dataListSelectionChangeHandle"
          @sort-change="dataListSortChangeHandle"
          border
          style="width: 100%;"
          v-loading="dataListLoading"
        >
          <el-table-column
            align="center"
            header-align="center"
            type="selection"
            width="50"
          />
          <el-table-column
            :label="$t('monitor.ip')"
            align="center"
            header-align="center"
            prop="ip"
          />
          <el-table-column
            :label="$t('monitor.port')"
            align="center"
            header-align="center"
            prop="port"
          />
          <el-table-column
            :label="$t('monitor.apiName')"
            align="center"
            header-align="center"
            prop="name"
          />
          <el-table-column
            :label="$t('monitor.url')"
            align="center"
            header-align="center"
            prop="url"
          />
          <el-table-column
            :label="$t('monitor.header')"
            align="center"
            header-align="center"
            prop="header"
          />
          <el-table-column
            :label="$t('monitor.method')"
            align="center"
            header-align="center"
            prop="method"
            sortable="custom"
          >
            <template slot-scope="scope">
              {{ scope.row.methodName }}
            </template>
          </el-table-column>

          <el-table-column
            :label="$t('monitor.monitorThreshold')"
            align="center"
            header-align="center"
            prop="monitorThreshold"
            sortable="custom"
          />
          <el-table-column
            :label="$t('monitor.params')"
            align="center"
            header-align="center"
            prop="params"
            sortable="custom"
          />
          <el-table-column
            :label="$t('monitor.status')"
            align="center"
            header-align="center"
            prop="status"
            sortable="custom"
          >
            <template slot-scope="scope">
              <el-tag
                size="small"
                type="success"
                v-if="scope.row.status"
              >{{ $t("user.status1") }}
              </el-tag>
              <el-tag
                size="small"
                type="danger"
                v-else>{{
                  $t("user.status0")
                }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('handle')"
            align="center"
            fixed="right"
            header-align="center"
            width="150"
          >
            <template slot-scope="scope">
              <el-button
                @click="addOrUpdateHandle(scope.row.id)"
                size="small"
                type="primary"
                v-if="$hasPermission('monitor:mmonitorapilist:update')"
              >{{ $t("update") }}
              </el-button>
              <el-button
                @click="deleteHandle(scope.row.id)"
                size="small"
                type="danger"
                v-if="$hasPermission('monitor:mmonitorapilist:delete')"
              >{{ $t("delete") }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="page"
          :page-size="limit"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          @current-change="pageCurrentChangeHandle"
          @size-change="pageSizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
        />
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update
          @refreshDataList="getDataList"
          ref="addOrUpdate"
          v-if="addOrUpdateVisible"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './api-add-or-update'
import { RequestMethod } from '@/utils/consts'

export default {
  name: 'ApiVue',
  mixins: [mixinViewModule],
  components: {
    AddOrUpdate
  },
  data () {
    return {
      ipList: [],
      loading: false,

      requestMethods: RequestMethod,
      mixinViewModuleOptions: {
        getDataListURL: '/monitor/mmonitorapilist/page',
        getDataListIsPage: true,
        deleteURL: '/monitor/mmonitorapilist/',
        deleteIsBatch: true
        // exportURL: '/sys/user/export'
      },
      dataForm: {
        username: ''
      }
    }
  },
  beforeMount () {},
  mounted () {
    this.getIpList()
  },
  methods: {
    handleResponseListData (data) {
      data = data.map(dataItem => {
        let name = ''
        Object.keys(RequestMethod).forEach(key => {
          if (RequestMethod[key] === dataItem.method) {
            name = key
          }
        })
        dataItem.methodName = name
        return dataItem
      })

      return data
    },
    getIpList () {
      this.loading = true

      this.$http
        .get('/monitor/mmonitorapilist/iplist')
        .then(({ data: res }) => {
          this.ipList = res.data.map(item => {
            return { id: item.id, label: item.ip, value: item.ip }
          })
        })
        .finally(() => {
          this.loading = false
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.sys-api-page {
}
</style>
